from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User,Group
from .models import Patient,Doctor,Receptionist,Inventory_manager,Lab_Doctor
from django.template.context_processors import csrf
from home.context_processors import hasGroup
from django.contrib import messages
import string


# Create your views here.
@login_required
def create_user(request):
    username = request.POST.get('username')
    if User.objects.filter(username=username).exists():
        messages.add_message(request, messages.ERROR, 'Username Already Exists.')
        return HttpResponseRedirect('/profile/register')
    password = request.POST.get('password1')
    cpassword = request.POST.get('password2')
    if not password == cpassword:
        messages.add_message(request, messages.ERROR, 'Passwords not matching.')
        return HttpResponseRedirect('/profile/register')
    first_name = request.POST.get('first_name')
    last_name = request.POST.get('last_name')
    email = request.POST.get('email')
    return_user = User.objects.create_user(username=username, password=password, first_name=first_name, last_name=last_name,
                                       email=email)
    return return_user


@login_required
def myProfile(request):
    c={}
    if hasGroup(request.user, 'patient'):
        c['isPatient'] = True
    return render(request, 'profiles/my_profile.html', c)

@login_required
def register(request):
    if hasGroup(request.user, 'receptionist') or request.user.is_superuser:
        c = {}
        c.update(csrf(request))
        return render(request, 'profiles/register.html')
    else:
        messages.add_message(request, messages.WARNING, 'Access Denied.')
        return HttpResponseRedirect('/home')

@login_required
def doRegister(request):
    if hasGroup(request.user, 'receptionist') and request.POST.get('occupation') == 'Patient':
        address = request.POST.get('address')
        dob = request.POST.get('dob')
        blood_group = request.POST.get('blood_group')
        patient = create_user(request)
        contact_no = request.POST.get('contact_no')
        if not contact_no.isdigit():
            messages.add_message(request, messages.ERROR, 'Wrong Contact no.')
            return HttpResponseRedirect('/profile/register')
        patient.patient = Patient(contact_no=int(contact_no), address=address, dob=dob, blood_group=blood_group)
        patient.patient.save()
        patient.save()

        try:
            group = Group.objects.get(name='patient')
        except:
            group = Group()
            group.name = 'patient'
            group.save()

        group = Group.objects.get(name='patient')
        group.user_set.add(patient)
        group.save()

        messages.add_message(request, messages.WARNING, 'Successfully Registered '+request.POST.get('username'))
        return HttpResponseRedirect('/case/generate')

    elif request.user.is_superuser and request.POST.get('occupation') != 'Patient':
        crew = create_user(request)
        oc = request.POST.get('occupation')
        contact_no = request.POST.get('contact_no')
        if not contact_no.isdigit():
            messages.add_message(request, messages.ERROR, 'Wrong Contact no.')
            return HttpResponseRedirect('/profile/register')
        if oc.rstrip(string.digits) == 'Doctor':
            crew.doctor = Doctor(contact_no = int(contact_no))
            crew.doctor.save()
            crew.save()
            try:
                group = Group.objects.get(name='doctor')
            except:
                group = Group()
                group.name = 'doctor'
                group.save()

            group.user_set.add(crew)
            group.save()

            try:
                group = Group.objects.get(name=oc.lower())
            except:
                group = Group()
                group.name = oc.lower()
                group.save()

        elif oc == 'Receptionist':
            crew.receptionist = Receptionist(contact_no = int(contact_no))
            crew.receptionist.save()
            crew.save()
            try:
                group = Group.objects.get(name='receptionist')
            except:
                group = Group()
                group.name = 'receptionist'
                group.save()

        elif oc == 'Inventory_manager':
            crew.inventory_manager = Inventory_manager(contact_no = int(contact_no))
            crew.inventory_manager.save()
            crew.save()
            try:
                group = Group.objects.get(name='inventory_manager')
            except:
                group = Group()
                group.name = 'inventory_manager'
                group.save()

        else:
            messages.add_message(request, messages.WARNING, 'Access Denied.')
            return HttpResponseRedirect('/home')

        group.user_set.add(crew)
        group.save()

        messages.add_message(request, messages.WARNING, 'Successfully Registered '+request.POST.get('username'))
        return HttpResponseRedirect('/home')

    else:
        messages.add_message(request, messages.WARNING, 'Access Denied.')
        return HttpResponseRedirect('/home')

